﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Millionaire;
using WindowsFormsApplication1.BLL;

namespace WindowsFormsApplication1
{
    public partial class FrmEditCauHoi : Form
    {

        /// <summary>
        /// giá trị true chỉ ra cell vừa được sửa đổi, false ngược lại
        /// </summary>
        private bool _propertyChanged = false;
        /// <summary>
        /// Lịch học cho sinh viên
        /// </summary>
        private DataTable dtCauHoi;
        private int currentIndex = -1;

        CauHoiBLL cauHoiBLL;
        LinhVucBLL linhVucBLL;
        LuaTuoiBLL luaTuoiBLL;

        public FrmEditCauHoi()
        {
            InitializeComponent();
            cauHoiBLL = new CauHoiBLL();
            linhVucBLL = new LinhVucBLL();
            luaTuoiBLL = new LuaTuoiBLL();
        }

        private void FrmEditCauHoi_Load(object sender, EventArgs e)
        {
            AddComboboxColumn();
            LoadDataToGridView();
            dgvCauHoi.Select();
        }

        private void LoadDataToGridView()
        {
            List<CauHoi> list = cauHoiBLL.GetAll();
            SetTableFromList(list);
            dgvCauHoi.DataSource = dtCauHoi;
            FormatGridView();
        }

        private void FormatGridView()
        {
            dgvCauHoi.Columns["MaCauHoi"].DisplayIndex = 1;
            dgvCauHoi.Columns["CauHoi"].DisplayIndex = 2;
            dgvCauHoi.Columns["MucDo"].DisplayIndex = 3;
            dgvCauHoi.Columns["cbLinhVuc"].DisplayIndex = 4;
            dgvCauHoi.Columns["cbLuaTuoi"].DisplayIndex = 5;
            dgvCauHoi.Columns["A"].DisplayIndex = 6;
            dgvCauHoi.Columns["B"].DisplayIndex = 7;
            dgvCauHoi.Columns["C"].DisplayIndex = 8;
            dgvCauHoi.Columns["D"].DisplayIndex = 9;
            dgvCauHoi.Columns["DapAn"].DisplayIndex = 10;

           dgvCauHoi.Columns["TenLinhVuc"].Visible = false;
           dgvCauHoi.Columns["TenLuaTuoi"].Visible = false; 

        }

        private void SetTableFromList(List<CauHoi> list)
        {
            dtCauHoi = new DataTable();
            dtCauHoi.Columns.Add("MaCauHoi",typeof(int));
            dtCauHoi.Columns.Add("CauHoi", typeof(string));
            dtCauHoi.Columns.Add("MucDo", typeof(int));
            dtCauHoi.Columns.Add("MaLinhVuc", typeof(int));
            dtCauHoi.Columns.Add("TenLinhVuc", typeof(string));
            dtCauHoi.Columns.Add("MaLuaTuoi", typeof(int));
            dtCauHoi.Columns.Add("TenLuaTuoi", typeof(string));
            dtCauHoi.Columns.Add("A", typeof(string));
            dtCauHoi.Columns.Add("B", typeof(string));
            dtCauHoi.Columns.Add("C", typeof(string));
            dtCauHoi.Columns.Add("D", typeof(string));
            dtCauHoi.Columns.Add("DapAn", typeof(string));
            foreach (var ch in list)
            {
                DataRow dr = dtCauHoi.NewRow();
                dr["MaCauHoi"] = ch.MaCauHoi;
                dr["CauHoi"] = ch.CauHoi1;
                dr["MucDo"] = ch.MucDo;
                dr["MaLinhVuc"]=ch.MaLinhVuc;
                dr["TenLinhVuc"] = ch.LinhVuc.TenLinhVuc;
                dr["MaLuaTuoi"] = ch.MaLuaTuoi;
                dr["TenLuaTuoi"] = ch.LuaTuoi.TenLuaTuoi;
                dr["A"] = ch.CauA;
                dr["B"] = ch.CauB;
                dr["C"] = ch.CauC;
                dr["D"] = ch.CauD;
                dr["DapAn"] = ch.DapAn;
                dtCauHoi.Rows.Add(dr);
            }
            dtCauHoi.AcceptChanges();

        }

        private void AddComboboxColumn()
        {
            //tạo comboBox phòng học
            DataGridViewComboBoxColumn cbLinhVuc = LoadColumnLinhVucc();
            dgvCauHoi.Columns.Add(cbLinhVuc);

            //tạo comboBox giảng viên
            DataGridViewComboBoxColumn cbLuaTuoi = LoadColumnLuaTuoi();
            dgvCauHoi.Columns.Add(cbLuaTuoi);

         
        }

        private DataGridViewComboBoxColumn LoadColumnLuaTuoi()
        {
            List<LuaTuoi> listLuaTuoi = luaTuoiBLL.GetAll();
            DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
            cb.Name = "cbLuaTuoi";
            cb.DataPropertyName = "MaLuaTuoi";
            cb.DataSource = listLuaTuoi;
            cb.DisplayMember = "TenLuaTuoi";
            cb.ValueMember = "MaLuaTuoi";
            return cb;
        }

        private DataGridViewComboBoxColumn LoadColumnLinhVucc()
        {
            List<LinhVuc> listLinhVuc = linhVucBLL.GetAll();
            DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
            cb.Name = "cbLinhVuc";
            cb.DataPropertyName = "MaLinhVuc";
            cb.DataSource = listLinhVuc;
            cb.DisplayMember = "TenLinhVuc";
            cb.ValueMember = "MaLinhVuc";
            return cb;
        }

        private void dgvCauHoi_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is ComboBox)
            {
                ComboBox cbBox = e.Control as ComboBox;
                cbBox.DropDownStyle = ComboBoxStyle.DropDown;
                cbBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                cbBox.AutoCompleteSource = AutoCompleteSource.ListItems;
            }
        }

        private void dgvCauHoi_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            _propertyChanged = true;
            currentIndex = e.RowIndex;
        }

        private void dgvCauHoi_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
        {
            try
            {
                if (e.Cell.RowIndex != currentIndex && _propertyChanged)
                {
                    _propertyChanged = false;

                    UpdateToDB();

                    dtCauHoi.AcceptChanges();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UpdateToDB()
        {
            //DataTable tbl = dataGridView1.DataSource as DataTable;
            DataTable changed = dtCauHoi.GetChanges();

            //update to db
            if (changed != null)
            {
                foreach (DataRow row in changed.Rows)
                {
                    switch (row.RowState)
                    {
                        case DataRowState.Added:
                            AddCauHoi(row);
                            break;
                        case DataRowState.Modified:
                            UpdateCauHoi(row);
                            break;
                        case DataRowState.Deleted:
                            break;
                    }
                }
                //LoadDataToGridView();
            }
        }

        private void UpdateCauHoi(DataRow row)
        {
            int maCh = (int)row["MaCauHoi"];
            string cauHoi = row["CauHoi"].ToString();
            int mucDo = (int)row["MucDo"];
            int maLV = (int)row["MaLinhVuc"];
            int maLT = (int)row["MaLuaTuoi"];
            CauHoi ch = new CauHoi()
            {
                MaCauHoi = maCh,
                CauHoi1 = cauHoi,
                MucDo = mucDo,
                MaLinhVuc = maLV,
                MaLuaTuoi = maLT,
                CauA = row["A"].ToString(),
                CauB = row["B"].ToString(),
                CauC = row["C"].ToString(),
                CauD = row["D"].ToString(),
                DapAn =  row["DapAn"].ToString()
            };
            cauHoiBLL.Update(ch);
        }

        private void AddCauHoi(DataRow row)
        {
            throw new NotImplementedException();
        }

        private void dgvCauHoi_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            dgvCauHoi.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
        }

        private void dgvCauHoi_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            dgvCauHoi.EndEdit();
        }
    }
}
